package cc.fs.myproject.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.View;

import cc.fs.myproject.utils.DensityUtils;
import cc.fs.myproject.utils.L;
import cc.fs.myproject.utils.T;

/**
 * Created by fostion on 2015/6/10.
 */
public class CustomView extends View {

    private final int SIZE = DensityUtils.dp2px(getContext(),50);
    private Paint mPaint;
    private String man = "♂";
    private ValueAnimator valueAnimator;
    private int pos,distance;


    public CustomView(Context context) {
        this(context, null);
    }

    public CustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    void init(){
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(SIZE);
        mPaint.setTextSize(SIZE);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int centX = getMeasuredWidth();
        int centY = getMeasuredHeight();
        distance = centX / 4;

        valueAnimator = ValueAnimator.ofInt(0,centX);
        valueAnimator.setDuration(7000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                pos = (int)animation.getAnimatedValue();
                invalidate();
            }
        });
        valueAnimator.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
          canvas.drawText(man,pos,pos,mPaint);
    }
}
